<?php

class Course_Manage {
    
    const TABLE_NAME = 'course';
    /**
     * db实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_dbInstance;
    /**
     * 单例对象
     */
    public static $singletonObjects;

    /**
     * 缓存的KEY
     */
    protected $_cacheKey;
    
     /**
     * 缓存命名空间
     */
    const CACHE_NS = '_course';
    
    /**
     * 获取单例
     */
    public static function getInstance() {
        $className = __CLASS__;
        if (!isset(self::$singletonObjects[$className])) {
            self::$singletonObjects[$className] = new self();
        }

        return self::$singletonObjects[$className];
    }

    /**
     * 构造器
     *
     * @param int $uId
     */
    protected function __construct() {
        $this->_cacheKey = self::CACHE_NS;
        $this->_cacheInstance = $this->_getCache();
//        $this->_data = $this->_cacheInstance->get( $this->_cacheKey );
//        if( $this->_data === false )
//        {
//            
//            //$this->_data = $this->loadFromDb();
//            
//            //保存到cache
//          //  $this->_saveToCache();
//        }
    }

    /*
     * 获取cache实例
     */

    public function _getCache() {
        return Common::getCache();
    }
    /**
     *添加cache
     * @param type $arr 
     */
    public function addCache ( $arr ) {

        $this->_cacheInstance->add(self::CACHE_NS, $arr);
    }
    /*
     * 获取redis实例
     */

    public function _getRedis() {
//        $config = array ('current'=>'127.0.0.1' , 'old' =>'6380') ;
//        $redis = new Helper_Redis($config);
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6380);
        return $redis;
    }

    /*
     * 获取db实例
     */

    public function _getMysql() {

        return Common::getMysql();
    }
    public function getCourse(){
         $courseInfo = Common::getMysql()->getList( self::TABLE_NAME);
         return $courserInfo;
        
    }
    /**
     * 添加课程
     */
    public function addCourse ( $courseName , $fcourse , $tag ) {
        if( empty( $courseName ) || empty(  $fcourse ) || empty( $tag)){
            throw new AppException(303);
            
        }

        $courseInfo = array( 
            'id'   =>Counter_Builder::getInstance()->getUniqueIdByKey( 'id' ),
            'name' => $courseName,
            'fid'  => $fcourse,
            'tag'  => $tag
            
        );
       $affectedRows = Common::getMysql()->add( self::TABLE_NAME ,$courseInfo );
        if( $affectedRows < 1 )
        {
            throw new AppException( 1000 );
        }
    }
    /**
     * 删除课程
     */
    public function delete( $id ){
       $delCourse = Common::getMysql()->delete( self::TABLE_NAME , array( 'id' => $id ));
        return $delCourse;
        
    }
    /**
     * 根据父id 查询课程
     * @param type $id
     * @return type 
     */
    public function courseList( $id ){
        $courseList = Common::getMysql()->getList( self::TABLE_NAME , array( 'id' => $id ));
        return $courseList; 
    }
    /**
     * 根据id 查询所对应的唯一课程信息
     * @param type $id
     * @return type 
     */
    public function courseOnly( $id ){
        $courseList = Common::getMysql()->get( self::TABLE_NAME , array( 'id' => $id ));
        return $courseList;     
    }
    
    public function edit( $id , $courseName , $fcourse ,$tag ){
        if( empty ( $id ) || empty ( $courseName ) || empty ( $fcourse ) || empty ( $tag)){
            throw new AppException( 303 );
        }
        $courseInfo = array( 
            'name' => $courseName,
            'fid'  => $fcourse,
            'tag'  => $tag
            
        );
       
         $affectedRows = Common::getMysql()->update( self::TABLE_NAME , array( 'id' => $id ) ,$courseInfo  );;
        if( $affectedRows < 1 )
        {
            throw new AppException( 1000 );
        }
        
    }
     /**
     * 从数据库加载课程的基本信息
     * @return array
     */
    private function _loadFromDb()
    {
        //获取用户基本信息
       $courseInfo = Indexer::getInstance( $this->uId )->getDb()->get( self::TABLE_NAME , array( 'uId' => $this->uId ) );

        if( $userInfo == false )
        {
            throw new AppException( 5 );
        }
        
        $this->_data = self::formatLngLat( $userInfo );
    }
     /**
     * 存储数据到cache
     */
    private function _saveToCache()
    {
        $this->_cacheInstance->set( $this->_cacheKey , $this->_data );
    }
     /*
     * 添加Cache
     * 
     */

    public function addCache ( $arr ) {

        $this->_cacheInstance->add(self::CACHE_NS, $arr);
    }

    /*
     * 获取cache
     */

    public function getCacheValue($cache_ns) {

        return $this->_cacheInstance->s($cache_ns);
    }

    /*
     * 插入cache
     * 
     */

    public function insertCache($pid) {
        $cache = $this->getCacheValue(self::CACHE_NS);
        $cache [$_SESSION [self::SESSION_ID]] [$pid] = array('Count' => 1, 'Date' => date('Y-m-d H:m:s', time()));
        $this->_cacheInstance->set(self::CACHE_NS, $cache);
    }
    
    
    
    
    
    
    
}
?>
